%reload_ext autoreload
%autoreload 2
%matplotlib inline
import fastai
from fastai.vision import *
#For CPU only
#fastai.torch_core.defaults.device = 'cpu'
#defaults.device= 'cpu'
path = Path('/home/jupyter/.fastai/data/banknotes/')
path_imgs=path/'imgs'
path_imgs.mkdir(parents=True, exist_ok=True)
path_imgs
src = (ImageList.from_folder(path_imgs,recurse=True)
.split_by_rand_pct(valid_pct=.2))
src
src.train.items[0]
#single class
func=lambda i: str(i.parent.relative_to(path_imgs) )
#multi class
func=lambda i: (i.parent.relative_to(path_imgs).parts )
func(src.train.items[0])
ll = src.label_from_func(func); ll
#ll = src.label_from_folder(); ll
tfms = get_transforms(do_flip=True,flip_vert=True,
max_rotate=90,
max_zoom=1.5,
max_lighting=0.5,
max_warp=0.5)
#so its reproducible
#np.random.seed(42)
def get_data(size,bs):
size=int(size)
bs=int(bs)
data = (ll.transform(tfms, size=size)
.databunch(bs=bs) #for CPU only add ,num_workers=0
.normalize(imagenet_stats))
return data
size,bs=256/2,20
data=get_data(size,bs)
data.show_batch(rows=4, figsize=(12,9))
arch = models.resnet50
acc_02 = partial(accuracy_thresh, thresh=0.2)
f_score = partial(fbeta, thresh=0.2)
#multiclass
learn = cnn_learner(data, arch, metrics=[acc_02, f_score])
#single class
#learn = cnn_learner(data, arch, metrics=[accuracy])
We use the LR Finder to pick a good learning rate.
learn.lr_find()
learn.recorder.plot()
Then we can fit the head of our network.
lr = 1e-2
learn.fit_one_cycle(10, slice(lr),callbacks=ShowGraph(learn))
learn.fit_one_cycle(5, slice(lr),callbacks=ShowGraph(learn))
learn.show_results(rows=3)
learn.save('stage-1-rn50')
...And fine-tune the whole model:
learn.unfreeze()
learn.lr_find()
learn.recorder.plot()
lr=1e-4
learn.fit_one_cycle(5, slice(1e-5, lr/5),callbacks=ShowGraph(learn))
learn.save('stage-2-rn50')
learn.fit_one_cycle(5, slice(1e-5, lr/5),callbacks=ShowGraph(learn))
learn.show_results(rows=3)
gc.collect()
torch.cuda.empty_cache()
size,bs=256,10/4
data=get_data(size,bs)
learn.freeze()
learn.lr_find()
learn.recorder.plot()
lr=5e-3
learn.fit_one_cycle(5, slice(lr),callbacks=ShowGraph(learn))
learn.fit_one_cycle(5, slice(lr),callbacks=ShowGraph(learn))
learn.save('stage-1-256-rn50')
learn.show_results()
learn.unfreeze()
learn.fit_one_cycle(5, slice(1e-5, lr/5),callbacks=ShowGraph(learn))
learn.save('stage-2-256-rn50')
learn.fit_one_cycle(5, slice(1e-5, lr/5),callbacks=ShowGraph(learn))
learn.save('stage-3-256-rn50')
learn.export()
path
# If using single class
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_top_losses(9, figsize=(15,11),heatmap=True)
interp.plot_confusion_matrix()